/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Comp_06_Slider.java]
 * Creation Date        [23-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing.components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Swing滑块组件
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Comp_06_Slider {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				SliderFrame frame = new SliderFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

/**
 * 滑块窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class SliderFrame extends JFrame {

	private static final long serialVersionUID = 8906964053396096948L;

	private static final int DEFAULT_WIDTH = 350;
	private static final int DEFAULT_HEIGHT = 450;

	private JTextField textField = null;
	private JPanel sliderPanel = null;
	private JSlider slider = null;

	private ChangeListener listener = null;

	/**
	 * 构造方法
	 */
	public SliderFrame() {
		setTitle("Slider Test");
		this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		setLocationByPlatform(true);
		setResizable(false);

		sliderPanel = new JPanel();

		// 滑块所需要的listener
		listener = new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent event) {
				JSlider slider = (JSlider) event.getSource(); // 获得JSlider
				textField.setText("" + slider.getValue());
			}
		};

		createCommonSlider(slider, "一般滑块");
		createTickedSlider(slider, "标尺滑块");
		createSnapedSlider(slider, "吸附滑块");
		createNoneTrackSlider(slider, "无轨滑块");
		createInvertedSlider(slider, "逆向滑块");
		createNumericSlider(slider, "数字滑块");
		createAlphabeticSlider(slider, "字母滑块");
		createImageIconSlider(slider, "图标滑块");

		textField = new JTextField();
		getContentPane().add(sliderPanel, BorderLayout.CENTER);
		getContentPane().add(textField, BorderLayout.SOUTH);

	}

	/**
	 * 创建一个基本的滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createCommonSlider(JSlider slider, String description) {
		slider = new JSlider(0, 100, 20);
		// slider = new JSlider(SwingConstants.VERTICAL, 0, 100, 20); 创建垂直滑块
		slider.addChangeListener(listener);// 添加监听器

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个带有大小标尺的滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createTickedSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.addChangeListener(listener);

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个带自动依附功能的滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createSnapedSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.setSnapToTicks(true);// 滑块自动移到最近的标尺处
		slider.addChangeListener(listener); // 添加监听器

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个无轨滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createNoneTrackSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.setPaintTrack(false);// 设置无轨
		slider.addChangeListener(listener); // 添加监听器

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个逆向滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createInvertedSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.setInverted(true);// 设置逆向
		slider.addChangeListener(listener); // 添加监听器

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个数字滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createNumericSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.setPaintLabels(true);// 显示滑块标尺标签
		slider.addChangeListener(listener); // 添加监听器

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个字母滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createAlphabeticSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.setPaintLabels(true);// 显示滑块标尺标签
		slider.addChangeListener(listener); // 添加监听器

		Dictionary<Integer, Component> labelTable = new Hashtable<Integer, Component>();
		labelTable.put(0, new JLabel("A"));
		labelTable.put(20, new JLabel("B"));
		labelTable.put(40, new JLabel("C"));
		labelTable.put(60, new JLabel("D"));
		labelTable.put(80, new JLabel("E"));
		labelTable.put(100, new JLabel("F"));
		slider.setLabelTable(labelTable);

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}

	/**
	 * 创建一个图标滑块
	 * 
	 * @param slider
	 *            滑块
	 * @param description
	 *            滑块的描述
	 */
	public void createImageIconSlider(JSlider slider, String description) {
		slider = new JSlider();
		slider.setPaintTicks(true);// 设置显示滑块标尺
		slider.setMajorTickSpacing(20);// 设置滑块大标尺标记范围
		slider.setMinorTickSpacing(5);// 设置滑块小标尺标记范围
		slider.setPaintLabels(true);// 显示滑块标尺标签
		slider.addChangeListener(listener); // 添加监听器

		Dictionary<Integer, Component> labelTable = new Hashtable<Integer, Component>();
		ImageIcon image = null;
		try {
			image = new ImageIcon(ImageIO.read(new File(
					"src_01_language_basic/pkg_resources/logo_01.png")));
		} catch (IOException e) {
			e.printStackTrace();
		}
		labelTable.put(0, new JLabel(image));
		labelTable.put(20, new JLabel(image));
		labelTable.put(40, new JLabel(image));
		labelTable.put(60, new JLabel(image));
		labelTable.put(80, new JLabel(image));
		labelTable.put(100, new JLabel(image));
		slider.setLabelTable(labelTable);

		JPanel panel = new JPanel();
		panel.add(slider);
		panel.add(new JLabel(description));

		sliderPanel.add(panel);
	}
}
